package com.lzc.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.plaf.basic.BasicScrollPaneUI.HSBChangeListener;

import org.apache.log4j.Logger;


import com.lzc.pojo.User;
import com.lzc.service.UserService;
import com.lzc.service.impl.UserServiceImpl;



/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
	Logger logger = Logger.getLogger(UserService.class);
	private static final long serialVersionUID = 1L;
	UserService us = new UserServiceImpl();
	@Override
		protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			//设置请求编码格式
			req.setCharacterEncoding("utf-8");
			//设置响应编码格式
			resp.setContentType("text/html;charset=utf-8");
			//接受命令
			String oper = req.getParameter("oper");
			System.out.println(oper);
			if ("login".equals(oper)) {
				checkUserLogin(req,resp);
			}else if ("reg".equals(oper)) {
				userReg(req,resp);
			}else if ("out".equals(oper)) {
				userOut(req,resp);
			}else if ("pwd".equals(oper)) {
				userChangePwd(req,resp);
			}else{
				logger.debug("没有找到对应的操作符"+oper);
				System.out.println("没有找到对应的操作符"+oper);
			}
			
			
		}
			//修改密码
			private void userChangePwd(HttpServletRequest req, HttpServletResponse resp) throws  ServletException, IOException {
		// TODO Auto-generated method stub
				String oldPwd = req.getParameter("oldpwd");
				String newPwd = req.getParameter("newpwd");
				//获取用户信息
				User u = (User)req.getSession().getAttribute("user");
				int uid = u.getUid();
				if(u.getPwd().equals(oldPwd)){
					System.out.println("修改密码");
					
		   			//处理请求
		   			//调用service处理
		   			int index = us.userChangePwdService(newPwd,uid);
		   			if (index>0) {
		   				//获取session对象
		   				HttpSession hs = req.getSession();
		   				hs.setAttribute("flag",1);
		   				resp.sendRedirect("/CRM-REC/index.jsp");
					}
				}else {//如果修改密码失败
					req.setAttribute("flag", 0);
					//请求转发
					//resp.getWriter().write("<script > alert();</script>");
					req.getRequestDispatcher("/main/views/set/user/password.jsp").forward(req, resp);
					
				}
				
		
	}
			//账户退出
			private void userOut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// TODO Auto-generated method stub
				// 获取session对象
	   			HttpSession hs = req.getSession();
	   			//强制销毁session
				hs.invalidate();
				//重定向到登录页面
				resp.sendRedirect("/CRM-REC/index.jsp");
		
	}
			//注册模块
			private void userReg(HttpServletRequest req, HttpServletResponse resp) throws IOException {
				// TODO Auto-generated method stub
				int flag = 0;
				String uname = req.getParameter("uname");
				String pwd = req.getParameter("pwd");
				String sex = req.getParameter("sex");
				int age = req.getParameter("age")!=""?Integer.parseInt(req.getParameter("age")):0;
				String birth = req.getParameter("birth");
				String[] bs = null;
				if (birth!="") {
					bs = birth.split("/");
					birth = bs[2]+"-"+bs[0]+"-"+bs[1];
				}
				
				System.out.println(uname+":"+pwd+":"+sex+":"+age+":"+birth+":"+flag);
				User u = new User(0, uname, pwd, sex, age, birth, flag);
				int index = us.userRegService(u);
				if (index>0) {
					HttpSession hs = req.getSession();
					hs.setAttribute("flag", 2);
					resp.sendRedirect("index.jsp");
				}
		
	}
			//登陆模块
			//处理登录
			private void checkUserLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
				// TODO Auto-generated method stub
				//获取请求信息
				String uname = req.getParameter("uname");
				String pwd = req.getParameter("pwd");
				System.out.println(uname+":"+pwd);
				//处理请求信息
				
				//校验
				 User u = us.checkUserLoginService(uname, pwd);
				 //System.out.println(u);
				 if (u!=null) {
					 //获取session对象
					 HttpSession hs =req.getSession();
					 //将用户数据存储到session中
					 hs.setAttribute("user", u);
					//重定向
					 resp.sendRedirect("main/views/index.jsp");
					 return;
				}else {
					//添加标识符到resquest
					req.setAttribute("flag", 0);
					//请求转发
					req.getRequestDispatcher("/index.jsp").forward(req, resp);
					return;
				}
				//响应请求信息
					//直接响应
					//请求转发
					
				 
			}

}
